//
//  YTCarCertificateViewController.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/7/7.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTCarCertificateViewController.h"
#import "YTCarsManagerViewController.h"
#import "YTCarAddViewModel.h"
#import "YTCarInfoViewModel.h"
#import "YTPickerView.h"

@interface YTCarCertificateViewController ()
@property (weak, nonatomic) IBOutlet UILabel *driverDateText;
@property (weak, nonatomic) IBOutlet UILabel *inspectionDateText;
@property (weak, nonatomic) IBOutlet UILabel *insuranceDateText;
@property (weak, nonatomic) IBOutlet UIButton *loadBtn;
@property (weak, nonatomic) IBOutlet UIButton *licenseBtn;
@property (weak, nonatomic) IBOutlet UIButton *inspectionBtn;
@property (weak, nonatomic) IBOutlet UIButton *insuranceBtn;
@end

@implementation YTCarCertificateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 更新
    if (_isUpdate) {
        _loadBtn.rac_command = _updateViewModel.submitCarInfo2;
        RAC(_updateViewModel, carDrivingLicenseTime) = RACObserve(_driverDateText, text);
        RAC(_updateViewModel, carInspectexpTime) = RACObserve(_inspectionDateText, text);
        RAC(_updateViewModel, carInsuranceTime) = RACObserve(_insuranceDateText, text);
    } else {
        _loadBtn.rac_command = _viewModel.submitCommand;
        RAC(_viewModel, carDrivingLicenseTime) = RACObserve(_driverDateText, text);
        RAC(_viewModel, carInspectexpTime) = RACObserve(_inspectionDateText, text);
        RAC(_viewModel, carInsuranceTime) = RACObserve(_insuranceDateText, text);
    }
    [self initialize];
}

- (void)initialize {
    @weakify(self);
    void (^color)(NSNumber *enable) = ^(NSNumber *enable) { // 提交按钮颜色
        @strongify(self);
        self.loadBtn.backgroundColor = (enable.boolValue ? kMainBlueColor : [UIColor lightGrayColor]);
    };
    void (^submit)(NSDictionary *response) = ^(NSDictionary *response) {
        @strongify(self);
        if ([response[@"resultCode"] integerValue] == 1000) {
            [SVProgressHUD showSuccessWithStatus:@"提交成功!\n我们会尽快审核"];
            for (UIViewController *vc in self.navigationController.viewControllers) {
                if ([vc isKindOfClass:[YTCarsManagerViewController class]]) {
                    YTCarsManagerViewController *manager = (YTCarsManagerViewController *)vc;
                    [self.navigationController popToViewController:manager animated:YES];
                }
            }
        }
    };
    if (_isUpdate) {
        [_updateViewModel.submitCarInfo2.enabled subscribeNext:color];
        [_updateViewModel.submitCarInfo2.executing subscribeNext:[self yt_showHudWithTips:@"拼命上传中..."]];
        [[_updateViewModel.submitCarInfo2.executionSignals switchToLatest] subscribeNext:submit];
    } else {
        [_viewModel.submitCommand.enabled subscribeNext:color];
        [_viewModel.submitCommand.executing subscribeNext:[self yt_showHudWithTips:@"拼命上传中..."]];
        [[_viewModel.submitCommand.executionSignals switchToLatest] subscribeNext:submit];
    }
}

#pragma mark - UITableView

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && indexPath.row == 0) {
        [self pickerDateWithLabel:_driverDateText tips:@"行驶证有效期"];
    }
    
    if (indexPath.section == 1 && indexPath.row == 0) {
        [self pickerDateWithLabel:_inspectionDateText tips:@"车辆年检有效期"];
    }
    
    if (indexPath.section == 2 && indexPath.row == 0) {
        [self pickerDateWithLabel:_insuranceDateText tips:@"车辆保险有效期"];
    }
}

- (void)pickerDateWithLabel:(UILabel *)label tips:(NSString *)tips {
    YTPickerView *picker = [YTPickerView pickerWithType:YTPickerTypeMonthDay];
    picker.dimmedMask = YES;
    picker.tips = tips;
    picker.completion = ^(NSString *selectString) {
        label.text = selectString;
    };
    [picker show];
}

- (IBAction)selecteImageAction:(UIButton *)sender {
    @weakify(self);
    [self yt_selecteImageCompletion:^(UIImage * _Nullable image) {
        @strongify(self);
        [sender setBackgroundImage:image forState:UIControlStateNormal];
        switch (sender.tag) {
            case 500: {
                if (_isUpdate) {
                    self.updateViewModel.drivingLicenseImage = image;
                } else {
                    self.viewModel.drivingLicenseImage = image;
                }
            }
                break;
            case 501: {
                if (_isUpdate) {
                    self.updateViewModel.carInspectexpImage = image;
                } else {
                    self.viewModel.carInspectexpImage = image;
                }
            }
                break;
            case 502: {
                if (_isUpdate) {
                    self.updateViewModel.insuranceImage = image;
                } else {
                    self.viewModel.insuranceImage = image;
                }
            }
                break;
        }
    }];
}

@end
